package controller

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/primitive"
	"github.com/apache/rocketmq-client-go/v2/producer"
	"github.com/gin-gonic/gin"
	"net/http"
	"order_bff/order_proto"
	"order_bff/pkg"
	"os"
	"strconv"
)

// 创建订单
func CreateOrder(c *gin.Context) {
	userId := c.PostForm("user_id")
	userIdInt, _ := strconv.Atoi(userId)
	goodsId := c.PostForm("goods_id")
	goodsIdInt, _ := strconv.Atoi(goodsId)
	stock := c.PostForm("stock")
	stockInt, _ := strconv.Atoi(stock)
	if len(userId) == 0 || len(goodsId) == 0 || len(stock) == 0 {
		c.JSON(http.StatusNotFound, gin.H{
			"code": -1,
			"msg":  "参数不能为空",
			"data": nil,
		})
		return
	}
	comment := pkg.Comment()
	order, err := comment.CreateOrder(context.Background(), &order_proto.CreateOrderRequest{
		UserId:  int32(userIdInt),
		GoodsId: int32(goodsIdInt),
		Stock:   int32(stockInt),
	})
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{
			"code": -1,
			"msg":  "创建订单失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "创建订单成功",
		"data": order.Id,
	})

}

func CreateOrders(c *gin.Context) {
	userId := c.PostForm("user_id")
	userIdInt, _ := strconv.Atoi(userId)
	goodsId := c.PostForm("goods_id")
	goodsIdInt, _ := strconv.Atoi(goodsId)
	stock := c.PostForm("stock")
	stockInt, _ := strconv.Atoi(stock)
	if len(userId) == 0 || len(goodsId) == 0 || len(stock) == 0 {
		c.JSON(http.StatusNotFound, gin.H{
			"code": -1,
			"msg":  "参数不能为空",
			"data": nil,
		})
		return
	}
	type Order struct {
		UserIdInt  int
		GoodsIdInt int
		StockInt   int
	}
	order := Order{
		UserIdInt:  userIdInt,
		GoodsIdInt: goodsIdInt,
		StockInt:   stockInt,
	}
	marshal, _ := json.Marshal(order)

	p, _ := rocketmq.NewProducer(
		producer.WithNsResolver(primitive.NewPassthroughResolver([]string{"10.3.189.13:9876"})),
		producer.WithRetry(2),
	)
	err := p.Start()
	if err != nil {
		fmt.Printf("start producer error: %s", err.Error())
		os.Exit(1)
	}
	topic := "test"

	msg := &primitive.Message{
		Topic: topic,
		Body:  marshal,
	}
	res, err := p.SendSync(context.Background(), msg)

	if err != nil {
		fmt.Printf("send message error: %s\n", err)
	} else {
		fmt.Printf("send message success: result=%s\n", res.String())
	}

	err = p.Shutdown()
	if err != nil {
		fmt.Printf("shutdown producer error: %s", err.Error())
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "创建订单成功",
		"data": nil,
	})

}

// 订单列表
func OrderList(c *gin.Context) {
	userId := c.Query("user_id")
	userIdInt, _ := strconv.Atoi(userId)
	page := c.Query("page")
	pageInt, _ := strconv.Atoi(page)
	limit := c.Query("limit")
	limitInt, _ := strconv.Atoi(limit)
	if len(userId) == 0 {
		c.JSON(http.StatusNotFound, gin.H{
			"code": -1,
			"msg":  "用户ID不能为空",
			"data": nil,
		})
		return
	}
	comment := pkg.Comment()
	list, err := comment.OrderList(context.Background(), &order_proto.OrderListRequest{
		UserId: int32(userIdInt),
		Page:   int32(pageInt),
		Limit:  int32(limitInt),
	})
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{
			"code": -1,
			"msg":  "订单列表展示失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "订单展示成功成功",
		"data": list,
	})
}

// 删除订单
func DeleteOrder(c *gin.Context) {
	orderSn := c.PostForm("order_sn")
	if orderSn == "" {
		c.JSON(http.StatusNotFound, gin.H{
			"code": -1,
			"msg":  "订单编号不能为空",
			"data": nil,
		})
		return
	}
	comment := pkg.Comment()
	_, err := comment.DeleteOrder(context.Background(), &order_proto.DeleteOrderRequest{OrderSn: orderSn})
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{
			"code": -1,
			"msg":  "删除订单失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "删除订单成功",
		"data": nil,
	})
}
